(function() {
    angular.module('app').component('attrPoolComponent', {
        templateUrl: 'app/controllers/attr.controller/attr.pool.component/attr.pool.component.html',
        controller: attrPoolComponent,
        controllerAs: 'vm',
        bindings: {
            filter: '=',
            property: '='
        }
    });

    attrPoolComponent.$inject = ['attrPoolComponentService', '$scope', '$rootScope'];

    function attrPoolComponent(attrPoolComponentService, $scope, $rootScope) {
        var vm = this;

        //vm.filter = {}; //filter bind
        //vm.property = ''; //属性 bind
        vm.seen = false;

        vm.table = [];

        vm.selected_props = []; //选择的属性

        vm.select = select; //function
        vm.unselect = unselect; //function

        var clearFun = $rootScope.$on('tool-search-component-emit-EV',function(e,filter){

            // console.log(filter);

            init(filter);
        });
        


        //get pro
        var clearProFun = $rootScope.$on('emit_attr_pool_component_EV', function(e, chooseProperty) {
            //bind
            vm.property = chooseProperty;

            if (vm.property.length == 0) {
                // console.log('vm.property is null;');
            } else {
                //传播数据给history和product
                $rootScope.$broadcast('emit_attr_product_component_EV', angular.copy(vm.filter), angular.copy(vm.property), "a");
                $rootScope.$broadcast('emit_attr_history_component_EV', angular.copy(vm.filter), angular.copy(vm.property), "a");
            }






            // console.log('--------------emit_attr_pool_component_EV $on------------------------');
            for (var i = vm.selected_props.length - 1; i > -1; i--) {
                unselect(i);
            }

            angular.forEach(vm.table, function(prop) {

                if (chooseProperty.indexOf(prop.propertyKey) != -1) {
                    angular.forEach(prop.propertys, function(property) {
                        if (chooseProperty.indexOf(property.key + ':' + property.name) != -1) {
                            property.selected = true;
                            vm.selected_props.push(property);
                        } else {
                            property.selected = false;
                        }
                    });
                }
            });
            // console.log(vm.selected_props);


        });
        $scope.$watch('vm.selected_props', function(newValue, oldValue) {

            // console.log('--------------vm.selected_props $watch------------------------');
            // if (angular.equals(newValue, oldValue)) {
            //     console.log('pool not select change');
            //     return;
            // }

            var selected_props = [];

            angular.forEach(vm.selected_props, function(prop, index) {
                selected_props.push(prop.key + ":" + prop.name);
            });

            if (selected_props.length > 0) {
                //bind
                vm.property = selected_props.join(',');
            } else {
                //bind
                vm.property = "";
            }

            if (vm.property.length == 0) {
                // console.log('vm.property is null;');
            } else {
                //传播数据给history和product
                $rootScope.$broadcast('emit_attr_product_component_EV', angular.copy(vm.filter), angular.copy(vm.property), "a");
                $rootScope.$broadcast('emit_attr_history_component_EV', angular.copy(vm.filter), angular.copy(vm.property), "a");
            }

            // //传播数据给history和product
            // $rootScope.$broadcast('emit_attr_product_component_EV', angular.copy(vm.filter), angular.copy(vm.property), "a");
            // $rootScope.$broadcast('emit_attr_history_component_EV', angular.copy(vm.filter), angular.copy(vm.property), "a");
        }, true);

        $scope.$on('$destroy', function() {
            clearFun();
            clearProFun();
        });

        function init(filter) {
            

            if (filter.category == "") {

                return;
            }

            if (!vm.seen) {
                vm.seen = true;
            }

            attrPoolComponentService.get(filter).then(function(data) {

                vm.table.length = 0;
                vm.table = dataParse(data);
                // console.log(vm.table);

            }, function(err) {

                vm.seen = false;
                $rootScope.$emit('pool_emit_hide_EV');
            });
        }


        function dataParse(data) {

            angular.forEach(data, function(item) {
                item.propertys = [];
                item.propertys_show = true; //默认show
                angular.forEach(item.propertyValue.split(','), function(str, index) {
                    item.propertys.push({
                        value: Number(str.split(':')[1]),
                        show: index > 4 ? false : true,
                        name: str.split(':')[0],
                        key: item.propertyKey
                    });
                });
                item.expand = function() {
                    if (!item.showall) {
                        angular.forEach(item.propertys, function(prop) {
                            prop.show = true;
                        });
                        item.showall = true;
                    } else {
                        angular.forEach(item.propertys, function(prop, index) {
                            if (index < 5) {
                                prop.show = true;
                            } else {
                                prop.show = false;
                            }
                        });
                        item.showall = false;
                    }
                }
            });
            data.sort(function(n, m) {
                return n.value - m.value;
            });

            return data;
        }


        function getSelect(prop, propertys) {
            for (var i = 0; i < propertys.length; i++) {
                if (propertys[i].propertyKey == prop.key) {
                    return propertys[i].propertys;
                }
            }
        }

        function select(prop) {
            if (prop.selected) {
                return;
            }

            // 遍历同一个属性列表中的其他属性
            var samelist = getSelect(prop, vm.table);
            var inserted = false;

            for (var i = 0; i < samelist.length; i++) {
                if (samelist[i].selected) {
                    samelist[i].selected = false;

                    // 遍历selected_props删除旧的元素
                    for (var j = 0; j < vm.selected_props.length; j++) {
                        if (vm.selected_props[j].key == samelist[i].key) {
                            vm.selected_props.splice(j, 1, prop);
                            inserted = true;
                        }
                    }
                }
            }

            // 一般情况下都插入到末尾
            if (!inserted) {
                // 如果已经满足三个,则删除第一个,并插入新元素到末尾
                if (vm.selected_props.length == 3) {
                    var _ = vm.selected_props.shift();
                    _.selected = false;
                }

                vm.selected_props.push(prop);
            }

            // 标志被选中
            prop.selected = true;
        }

        function unselect(index) {
            var _ = vm.selected_props.splice(index, 1);
            _[0].selected = false;

        }


        function clear(chooseProperty) {
            for (var i = vm.selected_props.length - 1; i > -1; i--) {
                unselect(i);
            }
            angular.forEach(vm.table, function(prop) {

                if (chooseProperty.indexOf(prop.propertyKey) != -1) {
                    angular.forEach(prop.propertys, function(property) {
                        if (chooseProperty.indexOf(property.key + ':' + property.name) != -1) {
                            property.selected = true;
                            vm.selected_props.push(property);
                        } else {
                            property.selected = false;
                        }
                    });

                }

            });
        }
    }
})();
